<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>调试页面 - 阿里宝宝</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; }
        .debug-section { margin: 20px 0; padding: 15px; border: 1px solid #ddd; }
        .success { color: green; }
        .error { color: red; }
        .info { color: blue; }
    </style>
</head>
<body>
    <h1>调试页面</h1>
    
    <div class="debug-section">
        <h3>1. 测试API连接</h3>
        <button onclick="testAPI()">测试API</button>
        <div id="apiResult"></div>
    </div>

    <div class="debug-section">
        <h3>2. 测试商品加载</h3>
        <button onclick="testProducts()">加载商品</button>
        <div id="productsResult"></div>
    </div>

    <div class="debug-section">
        <h3>3. 商品列表</h3>
        <div id="productsList"></div>
    </div>

    <script src="js/api.js"></script>
    <script>
        async function testAPI() {
            const result = document.getElementById('apiResult');
            try {
                const response = await fetch('http://localhost:8000/api/products');
                const data = await response.json();
                result.innerHTML = `<div class="success">✓ API正常: ${JSON.stringify(data).substring(0, 100)}...</div>`;
            } catch (error) {
                result.innerHTML = `<div class="error">✗ API错误: ${error.message}</div>`;
            }
        }

        async function testProducts() {
            const result = document.getElementById('productsResult');
            const list = document.getElementById('productsList');
            
            try {
                console.log('开始加载商品...');
                const response = await api.getProducts();
                console.log('API响应:', response);
                
                if (response.success) {
                    result.innerHTML = `<div class="success">✓ 加载成功: ${response.data.products.length}个商品</div>`;
                    
                    // 显示商品列表
                    list.innerHTML = response.data.products.map(product => `
                        <div style="border: 1px solid #ccc; margin: 10px 0; padding: 10px;">
                            <h4>${product.name}</h4>
                            <p>价格: ¥${product.price}</p>
                            <p>分类: ${product.category}</p>
                            <p>产地: ${product.location}</p>
                        </div>
                    `).join('');
                } else {
                    result.innerHTML = `<div class="error">✗ 加载失败: ${response.error}</div>`;
                }
            } catch (error) {
                console.error('加载商品失败:', error);
                result.innerHTML = `<div class="error">✗ 加载失败: ${error.message}</div>`;
            }
        }

        // 页面加载时自动测试
        window.addEventListener('load', () => {
            console.log('页面加载完成，开始测试...');
            testAPI();
        });
    </script>
</body>
</html>
